// order-combine-to-seat.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shoppingCart: {
      allPrice: 0,
      count: 0,
      people: 0,
      memo: '',
      list: []
    },
    people: 1,
    storeId: '',
    storeType: '',
    memo: '',
    merchant_id: '',
    seat_id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      merchant_id: options.merchant_id,
      seat_id: options.seat_id
    })
    console.log(this.data.merchant_id + " " + this.data.seat_id )
    this.loadOrder()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 添加用餐人数
   */
  addPeople: function () {
    var temp = this.data.shoppingCart
    temp.people = this.data.shoppingCart.people + 1
    this.setData({
      shoppingCart: temp
    })
  },
  /**
   * 减少用餐人数
   */
  minusPeople: function () {
    var temp = this.data.shoppingCart
    if (parseInt(this.data.shoppingCart.people) > 1) {
      temp.people = parseInt(this.data.shoppingCart.people) - 1
      this.setData({
        shoppingCart: temp
      })
    }
    else {
      wx.showModal({
        title: '提示',
        content: '就餐人数不能少于1',
        showCancel: false
      })
    }
  },
  // /**
  //  * 提交订单
  //  */
  // submit: function () {
  //   var path = '/api/v1/user_product/save?';
  //   var product = ''
  //   for (var i = 0; i < this.data.shoppingCart.list.length; i++) {
  //     if (parseInt(this.data.shoppingCart.list[i].num) > 0) {
  //       product += 'id=' + this.data.shoppingCart.list[i].data.id + '&qty=' + this.data.shoppingCart.list[i].num + ','
  //     }
  //   }
  //   path += 'merchant_id=' + this.data.storeId + '&people=' + this.data.people + '&product=' + encodeURIComponent(product) + '&memo=' + this.data.memo
  //   var that = this;
  //   getApp().http.request({
  //     url: path, //仅为示例，并非真实的接口地址
  //     method: 'POST',
  //     data: {},
  //     dataType: 'json',
  //     success: function (res) {
  //       console.log(res.data.data)
  //       if (res.data.code == '200') {
  //         console.log(res.data.data)
  //         app.globalData.in_shopping_cart = {}
  //         setTimeout(function () {
  //           wx.showToast({
  //             title: '订单保存成功',
  //           })
  //         }, 1000)
  //       }
  //       // //订单保存成功后清理购物车
  //       var userShoppingCart = getApp().globalData.shoppingCart
  //       //订单保存成功后清理购物车
  //       for (var i = 0; i < userShoppingCart.length; i++) {
  //         if (that.data.storeId == userShoppingCart[i].mid) {
  //           getApp().cleanShoppingCart(that.data.storeId, '点餐')
  //         }
  //       }
  //       wx.navigateBack({
  //         delta: 2
  //       })
  //       // wx.navigateTo({
  //       //   url: '../../pages/save-success/save-success?id=' + res.data.data.order_id,
  //       // })
  //     },
  //     fail: function () {

  //     }
  //   })
  // },
  /**
   * 备注信息
   */
  memoEvent: function (event) {
    this.setData({
      memo: event.detail.value
    })
  },
  /**
   * 添加商品进入购物车事件
   */
  addEvent: function (event) {
    var tmp_shoppingCart = this.data.shoppingCart
    tmp_shoppingCart.list[event.currentTarget.dataset.index].quantity += 1
    tmp_shoppingCart.allPrice += tmp_shoppingCart.list[event.currentTarget.dataset.index].price
    tmp_shoppingCart.count += 1
    this.setData({
      shoppingCart: tmp_shoppingCart
    })
  },
  /**
   * 从购物车删除商品事件
   */
  minusEvent: function (event) {
    var tmp_shoppingCart = this.data.shoppingCart
    console.log(tmp_shoppingCart.list)
    if (tmp_shoppingCart.list[event.currentTarget.dataset.index].quantity > 1) {
      tmp_shoppingCart.list[event.currentTarget.dataset.index].quantity -= 1
      tmp_shoppingCart.allPrice -= tmp_shoppingCart.list[event.currentTarget.dataset.index].price
      tmp_shoppingCart.count -= 1
      this.setData({
        shoppingCart: tmp_shoppingCart
      })
    }
    else {
      wx.showModal({
        title: '提示',
        content: '单品数量不能少于1',
        showCancel: false
      })
    }
  },
  /**
   * 加载商品到购物车
   */
  loadOrder: function () {
    var that = this
    getApp().http.request({
      url: "/api/v1/user_product/detail?merchant_id=" + that.data.merchant_id,
      data: {},
      success: function (res) {
        var temp = that.data.shoppingCart
        temp.list = res.data.data.products
        temp.memo = res.data.data.info['memo']
        temp.people = res.data.data.info['people']
        //计算总价
        for (var i = 0; i < res.data.data.products.length; i++) {
          temp.allPrice += res.data.data.products[i]['price'] * res.data.data.products[i]['quantity']
          temp.count += 1
        }
        that.setData({
          shoppingCart: temp
        })
        console.log(res.data)
      }
    })
  },
  /**
   * 将我的菜单保存到餐桌
   */
  combine: function () {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认添加菜单到餐桌？',
      success: function () {
        getApp().http.request({
          url: "/api/v1/user_product/to_menu?merchant_id=" + that.data.merchant_id + "&seat_id=" + that.data.seat_id,
          data: {},
          success: function (res) {
            console.log(res)
            if (res.statusCode == '200') {
              console.log(res.data.data)
              app.globalData.in_shopping_cart = {}
              setTimeout(function () {
                wx.showToast({
                  title: '菜单添加到餐桌成功',
                    duration:2000
                })
              }, 1000)
              console.log(that.data.merchant_id + ' ' + that.data.seat_id)
              getApp().http.request({
                url: "/api/v1/user_product/clear?merchant_id=" + that.data.merchant_id + "&seat_id=" + that.data.seat_id,
                data: {},
                success: function (res) {
                  console.log(res)
                  //订单保存成功后清理购物车
                  var userShoppingCart = getApp().globalData.shoppingCart
                  //订单保存成功后清理购物车
                  for (var i = 0; i < userShoppingCart.length; i++) {
                    if (that.data.merchant_id == userShoppingCart[i].mid) {
                      getApp().cleanShoppingCart(that.data.merchant_id, '点餐')
                    }
                  }

                  wx.navigateBack({
                    delta: 1
                  })
                }
              })
            }
          }
        })
      }
    })
  },
  cancel: function () {
    wx.navigateBack({
      delta: 1
    })
  }
})